package com.xingongchang.baolebei.adapter;

import org.json.JSONException;
import org.json.JSONObject;

import com.external.androidquery.callback.AjaxStatus;
import com.insthub.BeeFramework.model.BusinessResponse;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xingongchang.baolebei.BaolebeiApp;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.activity.CoinActivity;
import com.xingongchang.baolebei.activity.LetterActivity;
import com.xingongchang.baolebei.activity.ProfileActivity;
import com.xingongchang.baolebei.activity.SigninActivity;
import com.xingongchang.baolebei.activity.TopicActivity;
import com.xingongchang.baolebei.model.ProtocolConst;
import com.xingongchang.baolebei.model.UserInfoModel;
import com.xingongchang.baolebei.protocol.USER_INFO;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MorePopWindow extends PopupWindow implements OnClickListener{
	private View view;
	private LinearLayout layout_user, layout_topic, layout_letter, layout_coin;//layout_user  信息
	private SharedPreferences shared;
	private SharedPreferences.Editor editor;
	private String uid;
	private Activity mContex;
	private ImageView user_photo;//头像
	private TextView user_name;//昵称 
	private ImageLoader imageLoader = ImageLoader.getInstance();
	UserInfoModel mUserInfoModel;
	private ImageView more_img;
	int sign;
	
	public MorePopWindow(final Activity context,int i) {
		mContex = context;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = inflater.inflate(R.layout.forum_popup, null);
		layout_user = (LinearLayout) view.findViewById(R.id.layout_user);
		layout_topic = (LinearLayout) view.findViewById(R.id.layout_topic);
		layout_letter = (LinearLayout) view.findViewById(R.id.layout_letter);
		layout_coin = (LinearLayout) view.findViewById(R.id.layout_coin);
		user_photo = (ImageView) view.findViewById(R.id.user_photo);
		more_img= (ImageView) view.findViewById(R.id.more_img);
		user_name = (TextView) view.findViewById(R.id.user_name);
		shared = context.getSharedPreferences("userInfo", 0); 
		editor = shared.edit();
		if(i==1){
			more_img.setVisibility(View.VISIBLE);
		}else{
			more_img.setVisibility(View.GONE);
		}
		uid = shared.getString("uid", "");
		USER_INFO userinfo = BaolebeiApp.userinfo;
		if(userinfo != null){
			user_name.setText(userinfo.user_name);
			imageLoader.displayImage(userinfo.user_photo, user_photo);
		}else{
			user_photo.setImageResource(R.drawable.btn_user);
			user_name.setText("登录");
		}
		
		layout_user.setOnClickListener(this);
		layout_topic.setOnClickListener(this);
		layout_letter.setOnClickListener(this);
		layout_coin.setOnClickListener(this);
		int h = context.getWindowManager().getDefaultDisplay().getHeight();
		int w = context.getWindowManager().getDefaultDisplay().getWidth();
		// 设置SelectPicPopupWindow的View
		this.setContentView(view);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(w / 3 + 100);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		// 刷新状态
		this.update();
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0000000000);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		this.setBackgroundDrawable(dw);
		// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.ModePopupAnimation);

	}

	public void showPopupWindow(View parent) {
		if (!this.isShowing()) {
			this.showAsDropDown(parent, parent.getLayoutParams().width / 2, -18);
		} else {
			this.dismiss();
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		uid = shared.getString("uid", "");
		switch(v.getId()){
		
		case R.id.layout_user:
			if (uid.equals("")) {
				dismiss();
				Intent intent = new Intent(mContex, SigninActivity.class);
				mContex.startActivity(intent);
				mContex.overridePendingTransition(R.anim.push_buttom_in,R.anim.push_buttom_out);
			}else{
				dismiss();
				Intent intent1 = new Intent(mContex, ProfileActivity.class);
				mContex.startActivity(intent1);
			}
		
			break;
			
		case R.id.layout_topic:
			if (uid.equals("")) {
				dismiss();
				Intent intent = new Intent(mContex, SigninActivity.class);
				mContex.startActivity(intent);
				mContex.overridePendingTransition(R.anim.push_buttom_in,R.anim.push_buttom_out);
			}else{
				dismiss();
				Intent intent2 = new Intent(mContex, TopicActivity.class);
				mContex.startActivity(intent2);
			}
			break;
			
		case R.id.layout_letter:
			if (uid.equals("")) {
				dismiss();
				Intent intent = new Intent(mContex, SigninActivity.class);
				mContex.startActivity(intent);
				mContex.overridePendingTransition(R.anim.push_buttom_in,R.anim.push_buttom_out);
			}else{
				dismiss();
				Intent intent3 = new Intent(mContex, LetterActivity.class);
				mContex.startActivity(intent3);
			}
		
			break;
			
		case R.id.layout_coin:
			if (uid.equals("")) {
				dismiss();
				Intent intent = new Intent(mContex, SigninActivity.class);
				mContex.startActivity(intent);
				mContex.overridePendingTransition(R.anim.push_buttom_in,R.anim.push_buttom_out);
			}else{
				dismiss();
				Intent intent4 = new Intent(mContex, CoinActivity.class);
				mContex.startActivity(intent4);
			}
			break;
		}
	}
}
